package com.yaorange.module.association.service.impl;

import com.github.pagehelper.PageHelper;
import com.yaorange.ma_util.util.Md5Util;
import com.yaorange.ma_util.util.ThreadLocalUtil;
import com.yaorange.module.association.mapper.BsApplyMapper;
import com.yaorange.module.association.service.BsApplyService;
import com.yaorange.module.association.service.BsMemberService;
import com.yaorange.module.association.vo.BsMemberVO;
import com.yaorange.module.system.service.SysUserService;
import com.yaorange.pojo.BsApply;
import com.yaorange.pojo.BsMember;
import com.yaorange.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 入会申请记录Service实现类
 */
@Service
public class BsApplyServiceImpl implements BsApplyService {

    @Autowired
    private BsApplyMapper bsApplyMapper;

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private BsMemberService bsMemberService;

    @Override
    public List<BsApply> selectAll(BsApply bsApply) {
        return bsApplyMapper.selectAll(bsApply);
    }

    @Override
    public List<BsApply> selectPage(Integer pageNum, Integer pageSize, BsApply bsApply) {
        if (pageNum == null || pageSize == null) {
            // 如果分页参数为空，则不分页
            return selectAll(bsApply);
        }
        PageHelper.startPage(pageNum, pageSize);
        return bsApplyMapper.selectAll(bsApply);
    }

    @Override
    public BsApply selectById(Long id) {
        return bsApplyMapper.selectById(id);
    }

    @Override
    public int insert(BsApply bsApply) {
        return bsApplyMapper.insert(bsApply);
    }

    @Override
    public int updateById(BsApply bsApply) {
        return bsApplyMapper.updateById(bsApply);
    }

    @Override
    public int deleteById(Long id) {
        return bsApplyMapper.deleteById(id);
    }

    @Override
    public void audit(BsApply bsApply) {
        HashMap map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        SysUser sysUser = sysUserService.findById(Integer.valueOf(userId));
        //设置一些修改人信息
        bsApply.setApprUserId(Long.valueOf(sysUser.getUserId()));
        bsApply.setApprUserName(sysUser.getUserUsername());
        bsApply.setApprTime(new Date());
        //直接修改当前记录
        this.updateById(bsApply);
    }

    /**
     * 分配账号
     * @param bsMemberVO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void allocate(BsMemberVO bsMemberVO) {
        //1. 需要更新申请记录表.
        BsApply bsApply = new BsApply();
        bsApply.setId(bsMemberVO.getId());
        bsApply.setInitialAccount(bsMemberVO.getInitialAccount());
        bsApply.setInitialPassword(bsMemberVO.getInitialPassword());
        bsApply.setStatus(4);

        this.updateById(bsApply);

        //2. 需要将部分信息写入会员信息.注意要设置的字段(会员到期时间等等)
        BsApply bsApply1 = this.selectById(bsMemberVO.getId());
        BsMember bsMember = new BsMember();
        bsMember.setApplyId(bsMemberVO.getId());
        bsMember.setUnitName(bsApply1.getUnitName());
        bsMember.setUnitAddress(bsApply1.getUnitAddress());
        bsMember.setBusinessLicense(bsApply1.getBusinessLicense());
        bsMember.setCreditCode(bsApply1.getCreditCode());
        bsMember.setIntroduction(bsApply1.getIntroduction());
        bsMember.setMainBusiness(bsApply1.getMainBusiness());
        bsMember.setRegistrationTime(bsApply1.getRegistrationTime());
        bsMember.setRegisteredCapital(BigDecimal.valueOf(bsApply1.getRegisteredCapital()));
        bsMember.setApplicationForm(bsApply1.getApplicationForm());
        bsMember.setContactName(bsApply1.getContactName());
        bsMember.setContactPhone(bsApply1.getContactPhone());
        bsMember.setContactPosition(bsApply1.getContactPosition());
        bsMember.setContactEmail(bsApply1.getContactEmail());
        bsMember.setJoinTime(new Date());
        bsMember.setExpireTime(bsMemberVO.getExpireTime());
        bsMember.setShowFlag(0);
        bsMember.setStatus(1);
        bsMember.setDelFlag(0);
        bsMemberService.insert(bsMember);

        //3. 需要将用户名与密码写入user系统用户登录表.
        SysUser sysUser = new SysUser();
        sysUser.setUserUsername(bsMemberVO.getInitialAccount());
        sysUser.setUserPassword(Md5Util.md5(bsMemberVO.getInitialPassword()));
        HashMap map = ThreadLocalUtil.get();
        Integer userId =(Integer) map.get("userId");
        sysUser.setCreateBy(Long.valueOf(userId));
        sysUser.setCreateTime(LocalDateTime.now());
        sysUserService.save(sysUser);
    }
}
